
component  output="false" extends="AbsFactory"
{

	public PostFactory function init(){
		
		return this;
	}
	
	public it.millemultimedia.easyBlog.validator.PostValidator function getValidator(){
		
		return new it.millemultimedia.easyBlog.validator.PostValidator();
	}
	
	public it.millemultimedia.easyBlog.model.Post function createInstance(){
		
		return new it.millemultimedia.easyBlog.model.Post();
	}
	
	public it.millemultimedia.easyBlog.model.Post function get( required Query posts, required Query categories ){
		
		var list = createObject( 'java','java.util.ArrayList' ).init();
		var obj = createInstance() ;
		var end = 0 ;
		var i = 1 ;
		var c = 1 ;
		
		obj = build( arguments.posts, i, arguments.categories, c );
		
		return obj;
	}
	
	public it.millemultimedia.easyBlog.model.Post[] function list( required Query posts, required Query categories, Numeric start=0, Numeric count=0 ){
		
		var list = createObject( 'java','java.util.ArrayList' ).init();
		var obj = "";
		var page = {};
		var end = 0 ;
		var i = 1 ;
		var c = 1 ;
		
		
		
		if( arguments.start eq 0 and arguments.count eq 0 ){
		
			arguments.start = 1;
			end = arguments.posts.recordcount;
		
		}else{
		
			page = getPaging( arguments.posts.recordcount, arguments.start, arguments.count );
			
			end = page.endRow;
			arguments.start = page.startRow;
			
		}
		
		for( i=arguments.start; i<=end; i++ ){
		
			obj = build( arguments.posts, i, arguments.categories, c ) ;
			
			list.add( obj ) ;
		
		}
		
		return list;
		
	}
	
	private it.millemultimedia.easyBlog.model.Post function build( required Query posts, required Numeric index, Query categories, Numeric indexCategory ){
		
		var list = createObject( 'java','java.util.ArrayList' ).init();
		var objC = "";
		var obj = createInstance();
		var i = arguments.index;
		var c = arguments.indexCategory;
		
		if( structKeyExists( arguments.posts, 'Idpost' ) ){
		
		obj.setIdpost(arguments.posts.Idpost[i]);

		}
		if( structKeyExists( arguments.posts, 'Authorid' ) ){
		
		obj.setAuthorid(arguments.posts.Authorid[i]);

		}
		if( structKeyExists( arguments.posts, 'Status' ) ){
		
		obj.setStatus(arguments.posts.Status[i]);

		}
		if( structKeyExists( arguments.posts, 'Allowcomment' ) ){
		
		obj.setAllowcomment(arguments.posts.Allowcomment[i]);

		}
		if( structKeyExists( arguments.posts, 'Publishingdate' ) ){
		
		obj.setPublishingdate(arguments.posts.Publishingdate[i]);

		}
		if( structKeyExists( arguments.posts, 'Uuid' ) ){
		
		obj.setUuid(arguments.posts.Uuid[i]);

		}
		if( structKeyExists( arguments.posts, 'Title' ) ){
		
		obj.setTitle(arguments.posts.Title[i]);

		}
		if( structKeyExists( arguments.posts, 'Urlsafe' ) ){
		
		obj.setUrlsafe(arguments.posts.Urlsafe[i]);

		}
		if( structKeyExists( arguments.posts, 'Maincontent' ) ){
		
		obj.setMaincontent(arguments.posts.Maincontent[i]);

		}
		if( structKeyExists( arguments.posts, 'Excerpt' ) ){
		
		obj.setExcerpt(arguments.posts.Excerpt[i]);

		}
		if( structKeyExists( arguments.posts, 'name' ) ){
		
		obj.setAuthor(arguments.posts.name[i]);
		
		}
		if( structKeyExists( arguments.posts, 'commentCount' ) ){
		
		obj.setCommentCount(arguments.posts.commentCount[i]);
		
		}
		
		for( c=c; c<=arguments.categories.recordCount; c++ ){
		
			if( arguments.posts.Idpost[i] eq arguments.categories.Idpost[c] ){
	
				objC = new it.millemultimedia.easyBlog.model.Category() ;
				
				objC.setIdcategory(arguments.categories.Idcategory[c]);
				objC.setName(arguments.categories.Name[c]);
				objC.setDescription(arguments.categories.Description[c]);
				objC.setUuid(arguments.categories.Uuid[c]);
				
				list.add( objC ) ;

			}
		
		}
			
		obj.setCategories( list );
		
		return obj;
	}

}
